﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using dotnetCHARTING;
using ChartExtents;
using System.Collections.Generic;
using System.Drawing;

public partial class main_dataAnalyze_GTQX : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LineDAOs lineDAOs = LineDAOs.getInstance();
            List<String> lineNameList = lineDAOs.getLineNameList();
            for (int i = 0; i < lineNameList.Count; i++)
            {
                dropLine.Items.Add(lineNameList[i]);
            }
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            List<String> towerNameList = towerDAOs.getTowerNameList(lineNameList[0]);
            for (int j = 0; j < towerNameList.Count; j++)
            {
                dropTower.Items.Add(towerNameList[j]);
            }
            //TowerDAOs towerDAOs = TowerDAOs.getInstance();
            this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "X方向倾斜角");
            this.txtSXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "X方向倾斜角");
            this.txtHXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "Y方向倾斜角");
            this.txtSXQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "X方向倾斜度");
            this.txtHXQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "Y方向倾斜度");
            this.txtZHQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "杆塔倾斜度");
        }

    }
    //protected void btnHistory_Click(object sender, EventArgs e)
    //{
    //    TowerDAOs towerDAOs = TowerDAOs.getInstance();
    //    this.txtSXQXJ.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "X方向倾斜角", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtHXQXJ.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "Y方向倾斜角", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtSXQXD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "X方向倾斜度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtHXQXD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "Y方向倾斜度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtZHQXD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "杆塔倾斜度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);

    //    Drawing();
    //}
    protected void dropLine_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        dropTower.Items.Clear();
        List<String> towerNameList = towerDAOs.getcorrespondingTowerNameList_GTQX(dropLine.SelectedValue);
        for (int j = 0; j < towerNameList.Count; j++)
        {
            dropTower.Items.Add(towerNameList[j]);
        }
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "X方向倾斜角");
        this.txtSXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "X方向倾斜角");
        this.txtHXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "Y方向倾斜角");
        this.txtSXQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "X方向倾斜度");
        this.txtHXQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "Y方向倾斜度");
        this.txtZHQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "杆塔倾斜度");
    }
    private void Drawing(int i)
    {
       Chart[] chart={this.Chart1,this.Chart2};
       chart[i].Type = ChartType.Combo;//Horizontal;
       chart[i].Size = "600x350";
       chart[i].TempDirectory = "images";
       chart[i].Debug = true;
       chart[i].XAxis.TimeInterval = TimeInterval.Day;
       chart[i].XAxis.Scale = Scale.Time;
       SeriesCollection mySC;
       if (i == 0)
       {
           chart[i].YAxis.Name = "倾斜角";
           mySC = GetDataSource();
       }
       else
       {
           chart[i].YAxis.Name = "倾斜度";
           mySC = GetDataSourceB();
       }
        //chart[i].XAxis.
       chart[i].DefaultSeries.Type = SeriesType.Spline;
       chart[i].TitleBox.Position = TitleBoxPosition.FullWithLegend;
       chart[i].Series.StartDate = RadDatePicker1.SelectedDate.Value.Date;
       chart[i].Series.EndDate = RadDatePicker2.SelectedDate.Value.Date;
      // chart[i].DefaultSeries.Type = SeriesTypeFinancial.Close;
       //SeriesCollection mySC = GetDataSource();
       TowerDAOs towerDAOs = TowerDAOs.getInstance();
       DataSet ds = towerDAOs.getAlarm(dropTower.SelectedValue, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
       int j = ds.Tables[0].Rows.Count;
       String[] description = new String[j];
       for (int b = 0; b < j; b++) 
       {
           description[b] = ds.Tables[0].Rows[b]["act_description"].ToString();
           foreach (Element myE in mySC[0].Elements)
           {
               if (description[b].Equals("杆塔轻度倾斜"))
                   myE.Color = Color.Yellow;
               else if (description[b].Equals("杆塔中度倾斜"))
                   myE.Color = Color.Orange;
               else if (description[b].Equals("杆塔重度倾斜"))
                   myE.Color = Color.Red;
           }
           chart[i].XAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), ds.Tables[0].Rows[b]["start_time"], ds.Tables[0].Rows[b]["stop_time"]));
       }
       //towerDAOs.getHistoryMax(dropTower.SelectedValue, "杆塔倾斜度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
       //foreach (Element myE in mySC[0].Elements)
       //    if (myE.XDateTime > ds && myE.YValue < 100) myE.Color = Color.Red;
       //foreach (Element myE in mySC[1].Elements)
       //    if (myE.YValue > 40 && myE.YValue < 100) myE.Color = Color.Red;
       //foreach (Element myE in mySC[2].Elements)
       //    if (myE.YValue > 40 && myE.YValue < 100) myE.Color = Color.Red;
       // Add a couple axis markers.
       //chart[i].XAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), 40, 100));
       //Chart1.YAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), 0, 20));
      // chart[i].YAxis.Markers[0].LegendEntry.Visible = false;
       //Chart1.YAxis.Markers[1].LegendEntry.Visible = false;
       chart[i].SeriesCollection.Add(mySC);
        
    }
    private SeriesCollection GetDataSource()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds = new DataSet();
        int i = -1;
        for (int a = 1; a <= 3; a++)
        {
            if (i > 50)
            {
                i = 50;
            }
            Series s = new Series();
            if (a == 1)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "杆塔倾斜度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "综合倾斜度";
            }
            if (a == 2)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "X方向倾斜度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "顺线倾斜度";
            }
            if (a == 3)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "Y方向倾斜度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "横向倾斜度";
            }
            i = ds.Tables[0].Rows.Count;
            if (i > 0)
            {
                for (int b = 0; b < i; b++) //X轴尺度个数
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart2.Visible = true;
            }
        }
        //SC[0].DefaultElement.Color = Color.Blue;
        ////SC[1].DefaultElement.Color = Color.Red;
        //SC[1].DefaultElement.Color = Color.FromArgb(255, 99, 49);
        //SC[2].DefaultElement.Color = Color.FromArgb(0, 156, 255);
        return SC;
    }
    private SeriesCollection GetDataSourceB()
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds = new DataSet();
        int i = -1;
        for (int a = 1; a <= 2; a++)
        {
            if (i > 50)
            {
                i = 50;
            }
            Series s = new Series();
            if (a == 1)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "X方向倾斜角", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "顺线倾斜角";
            }
            if (a == 2)
            {
                ds = towerDAOs.getSpineB(dropTower.SelectedValue, "Y方向倾斜角", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
                s.Name = "横向倾斜角";
            }
            i = ds.Tables[0].Rows.Count;
            if (i > 0)
            {
                for (int b = 0; b < i; b++) //X轴尺度个数
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart2.Visible = true;
            }
        }
        //SC[0].DefaultElement.Color = Color.Blue;
        ////SC[1].DefaultElement.Color = Color.Red;
        //SC[1].DefaultElement.Color = Color.FromArgb(255, 99, 49);
        //SC[2].DefaultElement.Color = Color.FromArgb(0, 156, 255);
        return SC;
    }
    protected void dropTower_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "X方向倾斜角");
        this.txtSXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "X方向倾斜角");
        this.txtHXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "Y方向倾斜角");
        this.txtSXQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "X方向倾斜度");
        this.txtHXQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "Y方向倾斜度");
        this.txtZHQXD.Text = towerDAOs.getNow(dropTower.SelectedValue, "杆塔倾斜度");
    }
    protected void btnQuXian_Click(object sender, EventArgs e)
    {
        Drawing(0);
    }
    protected void btnQXD_Click(object sender, EventArgs e)
    {
        Drawing(1);
    }
}
